package com.example.havi.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.havi.R;
import com.example.havi.bean.CardBean;
import com.example.havi.bean.MessageBean;
import com.example.havi.helper.EaseChatRowVoicePlayer;
import com.example.havi.helper.GlideHelper;
import com.example.havi.helper.MessageTypes;
import com.example.havi.ui.EaseAlertDialog;
import com.example.havi.ui.chatitem.BaseChatViewHolder;
import com.example.havi.ui.chatitem.CardViewHolder;
import com.example.havi.ui.chatitem.FileViewHolder;
import com.example.havi.ui.chatitem.ImageViewHolder;
import com.example.havi.ui.chatitem.LocationViewHolder;
import com.example.havi.ui.chatitem.TextViewHolder;
import com.example.havi.ui.chatitem.VideoViewHolder;
import com.example.havi.ui.chatitem.VoiceViewHolder;
import com.example.havi.util.DateUtils;
import com.example.havi.util.GsonUtils;
import com.example.havi.util.LogUtil;
import com.tencent.imcore.TextElem;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMElem;
import com.tencent.imsdk.TIMElemType;
import com.tencent.imsdk.TIMImage;
import com.tencent.imsdk.TIMImageElem;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMMessageStatus;
import com.tencent.imsdk.TIMTextElem;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.ext.message.TIMConversationExt;
import com.tencent.imsdk.ext.message.TIMMessageExt;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.luck.picture.lib.config.PictureConfig.IMAGE;
import static com.luck.picture.lib.config.PictureConfig.VIDEO;
import static com.tencent.imsdk.TIMElemType.*;
import static com.tencent.imsdk.TIMMessageStatus.*;

public class ChatMessageAdapter extends RecyclerView.Adapter<BaseChatViewHolder> {

    private List<TIMMessage> allData = new ArrayList<>();
    private Context context;

    public ChatMessageAdapter(Context context) {
        this.context = context;
    }

    public List<TIMMessage> getAllData() {
        return allData;
    }

    public void updateData(List<TIMMessage> list) {
        allData.clear();
        allData.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(List<TIMMessage> list) {
        allData.addAll(0, list);
        notifyDataSetChanged();
    }

    /**
     * -------------------------------------------------1.ViewType handle start------------------------------------------------------------
     */
    @NonNull
    @Override
    public BaseChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return createItemViewHolder(parent, viewType);
    }

    private BaseChatViewHolder createItemViewHolder(ViewGroup parent, int viewType) {
        BaseChatViewHolder viewHolder = null;
        switch (viewType) {
            case MessageTypes.CARD_SEND:    //名片类型
                viewHolder = new CardViewHolder(LayoutInflater.from(context).inflate(R.layout.item_card_send, parent, false));
                break;
            case MessageTypes.CARD_RECEIVE:    //名片类型
                viewHolder = new CardViewHolder(LayoutInflater.from(context).inflate(R.layout.item_card_receive, parent, false));
                break;
            case MessageTypes.TXT_RECEIVE:
                viewHolder = new TextViewHolder(LayoutInflater.from(context).inflate(R.layout.item_txt_receive, parent, false));
                break;
            case MessageTypes.TXT_SEND:
                viewHolder = new TextViewHolder(LayoutInflater.from(context).inflate(R.layout.item_txt_send, parent, false));
                break;
            case MessageTypes.IMAGE_RECEIVE:
                viewHolder = new ImageViewHolder(LayoutInflater.from(context).inflate(R.layout.item_image_receive, parent, false));
                break;
            case MessageTypes.IMAGE_SEND:
                viewHolder = new ImageViewHolder(LayoutInflater.from(context).inflate(R.layout.item_image_send, parent, false));
                break;
            case MessageTypes.VOICE_RECEIVE:
                viewHolder = new VoiceViewHolder(LayoutInflater.from(context).inflate(R.layout.item_voice_receive, parent, false));
                break;
            case MessageTypes.VOICE_SEND:
                viewHolder = new VoiceViewHolder(LayoutInflater.from(context).inflate(R.layout.item_voice_send, parent, false));
                break;
            case MessageTypes.VIDEO_RECEIVE:
                viewHolder = new VideoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_video_receive, parent, false));
                break;
            case MessageTypes.VIDEO_SEND:
                viewHolder = new VideoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_video_send, parent, false));
                break;
            case MessageTypes.FILE_RECEIVE:
                viewHolder = new FileViewHolder(LayoutInflater.from(context).inflate(R.layout.item_file_receive, parent, false));
                break;
            case MessageTypes.FILE_SEND:
                viewHolder = new FileViewHolder(LayoutInflater.from(context).inflate(R.layout.item_file_send, parent, false));
                break;
            case MessageTypes.LOCATION_RECEIVE:
                viewHolder = new LocationViewHolder(LayoutInflater.from(context).inflate(R.layout.item_location_receive, parent, false));
                break;
            case MessageTypes.LOCATION_SEND:
                viewHolder = new LocationViewHolder(LayoutInflater.from(context).inflate(R.layout.item_location_send, parent, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public int getItemViewType(int position) {
        int messageType = MessageTypes.TXT_RECEIVE; //默认
        TIMMessage message = allData.get(position);
        //处理消息类型
        messageType = getBasicMessageType(messageType, message);
        return messageType;
    }

    private int getBasicMessageType(int messageType, TIMMessage message) {
        TIMElemType type = message.getElement(0).getType();
        switch (type) {
            case Text:
                messageType = message.isSelf() ? MessageTypes.TXT_SEND : MessageTypes.TXT_RECEIVE;
                break;
            case Image:
                messageType = message.isSelf() ? MessageTypes.IMAGE_SEND : MessageTypes.IMAGE_RECEIVE;
                break;
            case Sound:
                messageType = message.isSelf() ? MessageTypes.VOICE_SEND : MessageTypes.VOICE_RECEIVE;
                break;
            case Video:
                messageType = message.isSelf() ? MessageTypes.VIDEO_SEND : MessageTypes.VIDEO_RECEIVE;
                break;
            case Location:
                messageType = message.isSelf() ? MessageTypes.LOCATION_SEND : MessageTypes.LOCATION_RECEIVE;
                break;
            case File:
                messageType = message.isSelf() ? MessageTypes.FILE_SEND : MessageTypes.FILE_RECEIVE;
                break;
        }
        return messageType;
    }

    @Override
    public int getItemCount() {
        return allData.size();
    }


    /**
     * -------------------------------------------------2.ViewData bundle handle start------------------------------------------------------------
     */
    @Override
    public void onBindViewHolder(@NonNull BaseChatViewHolder holder, int position) {
        TIMMessage emMessage = allData.get(position);
        //显示基本信息
        setUpBaseView(holder, position, emMessage);
        //消息状态
        updateMessageStatus(holder, emMessage, position);
        //消息内容
        setUpMessageContent(holder, emMessage, position);
        //点击监听
        setUpClickListener(holder, emMessage, position);
    }

    //点击监听
    private void setUpClickListener(final BaseChatViewHolder holder, final TIMMessage message, final int position) {
        holder.bubbleLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {   //气泡点击
                listener.onItemViewClick(holder, v, 0, position, message);
            }
        });
        holder.userAvatarView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//头像点击
                listener.onItemViewClick(holder, v, 1, position, message);
            }
        });
        if (message.isSelf()) {
            holder.statusView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//发送状态点击
                    listener.onItemViewClick(holder, v, 2, position, message);
                }
            });
        }
    }

    //消息内容更新
    private void setUpMessageContent(@NonNull BaseChatViewHolder holder, TIMMessage message, int position) {
//        Map<String, Object> map = message.ext();
//        MessageBean messageBean = MessageBean.mapToBean(map);
        TIMElemType type = message.getElement(0).getType();
        if (type == TIMElemType.Custom) {
//            EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
//            CardViewHolder cardViewHolder = (CardViewHolder) holder;
//            CardBean cardBean = GsonUtils.jsonToBean(txtBody.getMessage(), CardBean.class);
//            cardViewHolder.card_name_tv.setText(cardBean.getNickName());
//            GlideHelper.INSTANCE.loadImage(cardViewHolder.card_avatar_iv,cardBean.getAvatar());
//            cardViewHolder.card_company_tv.setText(cardBean.getCompany()+" "+cardBean.getJob());
        } else {
            setUpBasicMessageContent(holder, message);
        }

    }

    private void setUpBasicMessageContent(@NonNull BaseChatViewHolder holder, TIMMessage message) {
        TIMElem timElem = message.getElement(0);
        TIMElemType type = timElem.getType();
        switch (type) {
            case Text:
                TIMTextElem elem = (TIMTextElem)timElem;
                // 设置内容
                ((TextViewHolder) holder).contentView.setText(elem.getText());
                break;
            case Image:
                loadImage(holder, message);
                break;
            case Sound:
//                loadVoice(holder, message);
                break;
            case Video:
                break;
            case Location:
                break;
            case File:
                break;
        }
    }

//    private void loadVoice(BaseChatViewHolder holder, EMMessage message) {
//        VoiceViewHolder voiceViewHolder = (VoiceViewHolder) holder;
//        EMVoiceMessageBody voiceBody = (EMVoiceMessageBody) message.getBody();
//        int len = voiceBody.getLength();
//        if (len > 0) {
//            voiceViewHolder.voiceLengthView.setText(voiceBody.getLength() + "\"");
//            voiceViewHolder.voiceLengthView.setVisibility(View.VISIBLE);
//        } else {
//            voiceViewHolder.voiceLengthView.setVisibility(View.INVISIBLE);
//        }
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
//            voiceViewHolder.voiceImageView.setImageResource(R.drawable.ease_chatfrom_voice_playing);
//            if (message.isListened()) {
//                // hide the unread icon
//                voiceViewHolder.readStatusView.setVisibility(View.INVISIBLE);
//            } else {
//                voiceViewHolder.readStatusView.setVisibility(View.VISIBLE);
//            }
//            if (voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING ||
//                    voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) {
//                if (EMClient.getInstance().getOptions().getAutodownloadThumbnail()) {
//                    voiceViewHolder.progressBar.setVisibility(View.VISIBLE);
//                } else {
//                    voiceViewHolder.progressBar.setVisibility(View.INVISIBLE);
//                }
//
//            } else {
//                voiceViewHolder.progressBar.setVisibility(View.INVISIBLE);
//            }
//        } else {
//            if (voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING ||
//                    voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) {
//                voiceViewHolder.progressBar.setVisibility(View.VISIBLE);
//            } else {
//                voiceViewHolder.progressBar.setVisibility(View.INVISIBLE);
//            }
//            voiceViewHolder.voiceImageView.setImageResource(R.drawable.ease_chatto_voice_playing);
//        }
//        // To avoid the item is recycled by listview and slide to this item again but the animation is stopped.
//        EaseChatRowVoicePlayer voicePlayer = EaseChatRowVoicePlayer.getInstance(context);
//        if (voicePlayer.isPlaying() && message.getMsgId().equals(voicePlayer.getCurrentPlayingId())) {
//            startVoicePlayAnimation(voiceViewHolder, message);
//        } else {
//            stopVoicePlayAnimation(voiceViewHolder, message);
//        }
//    }
//
//    AnimationDrawable voiceAnimation;
//
//    public void startVoicePlayAnimation(VoiceViewHolder voiceViewHolder, EMMessage message) {
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
//            voiceViewHolder.voiceImageView.setImageResource(R.drawable.voice_from_icon);
//        } else {
//            voiceViewHolder.voiceImageView.setImageResource(R.drawable.voice_to_icon);
//        }
//        voiceAnimation = (AnimationDrawable) voiceViewHolder.voiceImageView.getDrawable();
//        voiceAnimation.start();
//
//        // Hide the voice item not listened status view.
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
//            voiceViewHolder.readStatusView.setVisibility(View.INVISIBLE);
//        }
//    }
//
//    public void stopVoicePlayAnimation(VoiceViewHolder voiceViewHolder, EMMessage message) {
//        if (voiceAnimation != null) {
//            voiceAnimation.stop();
//            //停止播放后置空
//            voiceAnimation = null;
//        }
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
//            voiceViewHolder.voiceImageView.setImageResource(R.drawable.ease_chatfrom_voice_playing);
//        } else {
//            voiceViewHolder.voiceImageView.setImageResource(R.drawable.ease_chatto_voice_playing);
//        }
//    }


    private void loadImage(BaseChatViewHolder holder, TIMMessage msg) {
        ImageViewHolder imageHolder = (ImageViewHolder) holder;
        //遍历一条消息的元素列表
        for(int i = 0; i < msg.getElementCount(); ++i) {
            TIMElem elem = msg.getElement(i);
            if (elem.getType() == TIMElemType.Image) {
                //图片元素
                TIMImageElem e = (TIMImageElem) elem;
                for(TIMImage image : e.getImageList()) {

                    //获取图片类型, 大小, 宽高
                    Log.d("mango", "image type: " + image.getType() +
                            " image size " + image.getSize() +
                            " image height " + image.getHeight() +
                            " image width " + image.getWidth());
                    imageHolder.progressBar.setVisibility(View.GONE);
                    imageHolder.percentageView.setVisibility(View.GONE);
                    if (msg.isSelf()) {
                        GlideHelper.INSTANCE.loadImage(((ImageViewHolder) holder).imageView, image.getUrl(), 500, 500);
                    } else {
                        GlideHelper.INSTANCE.loadImage(((ImageViewHolder) holder).imageView, image.getUrl(), 500, 500);
                    }
                }
            }
        }


    }

    //消息状态更新
    private void updateMessageStatus(BaseChatViewHolder holder, TIMMessage emMessage, int position) {
        if (emMessage.isSelf()) {
            switch (emMessage.status()) {
                case SendSucc:
                    onMessageSuccess(holder);
                    break;
                case SendFail:
                    onMessageError(holder);
                    break;
                case Sending:
                    onMessageInProgress(holder);
                    break;
            }
        } else if (!emMessage.isSelf()) {
            TIMMessageExt ext = new TIMMessageExt(emMessage);
            TIMConversation conversation = emMessage.getConversation();
            if (!ext.isRead() && conversation.getType() == TIMConversationType.C2C) {
                //设置消息已读
                //获取会话扩展实例
                TIMConversationExt conExt = new TIMConversationExt(conversation);
                conExt.setReadMessage(emMessage, new TIMCallBack() {
                    @Override
                    public void onError(int code, String desc) {
                        Log.e("mango", "setReadMessage failed, code: " + code + "|desc: " + desc);
                    }

                    @Override
                    public void onSuccess() {
                        Log.d("mango", "setReadMessage succ");
                    }
                });
            }
        }

    }

    private void onMessageCreate(BaseChatViewHolder holder) {
        holder.progressBar.setVisibility(View.VISIBLE);
        holder.statusView.setVisibility(View.GONE);
    }

    private void onMessageSuccess(BaseChatViewHolder holder) {
        holder.progressBar.setVisibility(View.GONE);
        holder.statusView.setVisibility(View.GONE);
    }

    private void onMessageError(BaseChatViewHolder holder) {
        holder.progressBar.setVisibility(View.GONE);
        holder.statusView.setVisibility(View.VISIBLE);
    }

    private void onMessageInProgress(BaseChatViewHolder holder) {
        holder.progressBar.setVisibility(View.VISIBLE);
        holder.statusView.setVisibility(View.GONE);
    }

    //显示基本信息
    private void setUpBaseView(@NonNull BaseChatViewHolder holder, int position, TIMMessage emMessage) {
//        if (emMessage.direct() == EMMessage.Direct.SEND) {
//            if (EMClient.getInstance().getOptions().getRequireDeliveryAck()) {
//                if (emMessage.isDelivered()) {
//                    holder.deliveredView.setVisibility(View.VISIBLE);
//                } else {
//                    holder.deliveredView.setVisibility(View.INVISIBLE);
//                }
//            }
//            if (EMClient.getInstance().getOptions().getRequireAck()) {
//                if (emMessage.isAcked()) {
//                    if (holder.deliveredView != null) {
//                        holder.deliveredView.setVisibility(View.INVISIBLE);
//                    }
//                    holder.ackedView.setVisibility(View.VISIBLE);
//                } else {
//                    holder.ackedView.setVisibility(View.INVISIBLE);
//                }
//            }
//        }

        //时间
        if (position == 0) {
            holder.timeStampView.setText(DateUtils.timeMachineRecordTime(emMessage.timestamp()));
            holder.timeStampView.setVisibility(View.VISIBLE);
        } else {
            // show time stamp if interval with last message is > 30 seconds
            TIMMessage prevMessage = (TIMMessage) allData.get(position - 1);
            if (prevMessage != null && DateUtils.isCloseEnough(emMessage.timestamp(), prevMessage.timestamp())) {
                holder.timeStampView.setVisibility(View.GONE);
            } else {
                holder.timeStampView.setText(DateUtils.timeMachineRecordTime(emMessage.timestamp()));
                holder.timeStampView.setVisibility(View.VISIBLE);
            }
        }
//        基本信息 头像等信息 通过message的sender获取
//        Map<String, Object> map = emMessage.ext();
//        MessageBean messageBean = MessageBean.mapToBean(map);
        TIMUserProfile senderProfile = emMessage.getSenderProfile();
        GlideHelper.INSTANCE.loadCircleImage(holder.userAvatarView, senderProfile.getFaceUrl());
        // 获取conversation的type
        TIMConversationType type = emMessage.getConversation().getType();
        if (type == TIMConversationType.Group) {
            // 群聊或者聊天室 显示昵称
            if (holder.usernickView != null) {
                holder.usernickView.setVisibility(View.VISIBLE);
                holder.usernickView.setText(senderProfile.getNickName());
            }
        } else {
//            个人聊天 不显示昵称
            holder.usernickView.setVisibility(View.GONE);
        }

    }


    /**
     * -------------------------------------------------3.ViewItem click listener start------------------------------------------------------------
     */
    OnItemViewClickListener listener;

    public void setOnItemViewClickListener(OnItemViewClickListener l) {
        listener = l;
    }

    public interface OnItemViewClickListener {
        void onItemViewClick(BaseChatViewHolder holder, View view, int ViewType, int position, TIMMessage emMessage);
    }
}
